#!/usr/bin/env python
# encoding: utf-8
# Date: 2017-07-25
# file:views.py
# Email: wang521kang@outlook.com
# Author: 王康
from commodity.models import Commodity, CommodityImg
from order.models import Order, OrderCommodity
from utils.views import get_json, request_parms_check


def query_send_order(request):
    orders = Order.objects.filter(status=1).all()
    l = []
    res = {}
    for order in orders:
        res['id'] = order.id
        res['create_time'] = order.create_time.strftime('%Y-%m-%d %H:%M:%S')
        res['name'] = order.name
        res['phone'] = order.phone
        res['add_province'] = order.add_province
        res['add_city'] = order.add_city
        res['add_district'] = order.add_district
        res['add_detail'] = order.add_detail
        commoditys = OrderCommodity.objects.filter(order_id=order.id).all()
        lst = []
        res['commodity'] = lst
        for commodity in commoditys:
            com = {}
            com['price'] = commodity.price
            com['num'] = commodity.num
            obj = Commodity.objects.get(id=commodity.id)
            com['name'] = obj.name
            com['classification'] = obj.commodity_classification.classification
            commodityimg = CommodityImg.objects.filter(commodity_id=commodity.id).all()
            if commodityimg:
                com['img_url'] = commodityimg.img_url
            lst.append(com)
        l.append(res)
    return get_json(True, '', l)


def query_send_order_over(request):
    orders = Order.objects.filter(status=2).all().order_by('-create_time')
    l = []
    res = {}
    for order in orders:
        res['id'] = order.id

        res['create_time'] = order.create_time.strftime('%Y-%m-%d %H:%M:%S')
        res['name'] = order.name
        res['phone'] = order.phone
        res['add_province'] = order.add_province
        res['add_city'] = order.add_city
        res['add_district'] = order.add_district
        res['add_detail'] = order.add_detail
        commoditys = OrderCommodity.objects.filter(order_id=order.id).all()
        lst = []
        res['commodity'] = lst
        for commodity in commoditys:
            com = {}
            com['price'] = commodity.price
            com['num'] = commodity.num
            obj = Commodity.objects.get(id=commodity.id)
            com['name'] = obj.name
            com['classification'] = obj.commodity_classification.classification
            commodityimg = CommodityImg.objects.filter(commodity_id=commodity.id).all()
            if commodityimg:
                com['img_url'] = commodityimg.img_url
            lst.append(com)
        l.append(res)
    return get_json(True, '', l)


@request_parms_check(order_id=('订单id', 'r,int'))
def get_send_order(request, order_id):
    order = Order.objects.get(id=order_id)
    res = {}
    res['id'] = order.id
    res['status'] = order.status
    res['create_time'] = order.create_time.strftime('%Y-%m-%d %H:%M:%S')
    res['name'] = order.name
    res['phone'] = order.phone
    res['add_province'] = order.add_province
    res['add_city'] = order.add_city
    res['add_district'] = order.add_district
    res['add_detail'] = order.add_detail
    commoditys = OrderCommodity.objects.filter(order_id=order.id).all()
    lst = []
    res['commodity'] = lst
    for commodity in commoditys:
        com = {}
        com['price'] = commodity.price
        com['num'] = commodity.num
        obj = Commodity.objects.get(id=commodity.id)
        com['name'] = obj.name
        com['classification'] = obj.commodity_classification.classification
        commodityimgs = CommodityImg.objects.filter(commodity_id=commodity.id).all()
        if commodityimgs:
            com['img_url'] = commodityimgs[0].img_url
        lst.append(com)
    return get_json(True, '', res)
